﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ServiceModelEx;
using CloudObserverDB;

namespace CloudObserverService
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // ICloudObserver _proxy;

        public MainWindow()
        {
            InitializeComponent();
            //_proxy = InProcFactory.CreateInstance<CloudObserver.CloudObserver, ICloudObserver>();
        }

        private void observe_Click(object sender, RoutedEventArgs e)
        {
            Observe();
        }

        private void Observe()
        {
            var queryString = (customQuery.Text.Trim().Length == 0 ? (string)((ComboBoxItem)query.SelectedValue).Content : customQuery.Text);

            System.Threading.Thread.Sleep(2000);


            try
            {
                // _proxy.Observe(queryString, int.Parse(count.Text), int.Parse(duration.Text));
                CloudObserverAgent.CloudObserverLogic col = new CloudObserverAgent.CloudObserverLogic();
                status.Text = "Observing at " + System.DateTime.Now.ToString() + " " + queryString + "...";
                col.Observe(queryString, int.Parse(count.Text), int.Parse(duration.Text));
                status.Text = "Finished observing at " + System.DateTime.Now.ToString() + " " + queryString + ".";
            }
            catch (Exception ex)
            {

                System.Diagnostics.EventLog.WriteEntry("CloudObserver", ex.Message);
                status.Text = ex.Message;

                System.Threading.Thread.Sleep(50000);
                Observe();
            }
        }

        private void query_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            customQuery.IsEnabled = query.SelectedIndex == 0;
        }
    }
}
